"""
简单的 cast 语句替换
"""

import re
import logging


def replace_simple_cast_string(m):
    """
    cast([] as [STRING|string] ) 转化为 cast([] as [TEXT] )
    :param m: 。
    :return: 。
    """
    new_date_format = "{}({}{} TEXT)".format(m.group(1), m.group(2), m.group(3))
    return new_date_format

def replace_simple_cast_tinyint(m):
    """
    cast([] as [TINYINT|tinyint] ) 转化为 cast([] as [SMALLINT] )
    :param m: 。
    :return: 。
    """
    new_date_format = "{}({}{} SMALLINT)".format(m.group(1), m.group(2), m.group(3))
    return new_date_format

def replace_simple_cast_type(all_the_text):
    unix_timestamp_patten = re.compile(r"(CAST|cast)\((.*)(AS|as) (STRING|string)\)")
    result = unix_timestamp_patten.sub(replace_simple_cast_string, all_the_text)
    all_the_text = result

    unix_timestamp_patten = re.compile(r"(CAST|cast)\((.*)(AS|as) (TINYINT|tinyint)\)")
    result = unix_timestamp_patten.sub(replace_simple_cast_tinyint, all_the_text)
    all_the_text = result

    unix_timestamp_patten = re.compile(r"(CAST|cast)\((.*)(AS|as) (TINYINT|tinyint) \)")
    result = unix_timestamp_patten.sub(replace_simple_cast_tinyint, all_the_text)
    all_the_text = result

    return all_the_text





